package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.Paper;
import com.neuedu.service.PaperService;

@Controller
@RequestMapping("/admin/paper")
public class PaperAction {

	@Autowired
	private PaperService PaperService;

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("startPage", (page - 1) * rows);
		pageMap.put("endPage", rows);

		List<Paper> u = PaperService.findAll(pageMap);
		int total = PaperService.countPaper();
		map.put("rows", u);
		map.put("total", total);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="addPaper")
	public void addUserMessage(Paper Paper, HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 调用业务逻辑层进行处理，返回数据库表数据更新的行数：更新成功数量大于等于1，更新失败返回0
		int res = PaperService.addPaper(Paper);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);

	}
	
	@RequestMapping(value="updatePaper")
	public void updateUserMessage(Paper Paper, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 调用业务逻辑层进行处理，返回数据库表数据更新的行数：更新成功数量大于等于1，更新失败返回0
		int res = PaperService.updatePaper(Paper);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="deletePaper")
	public void deletePaper(Paper Paper,HttpServletRequest request,HttpServletResponse response) throws Exception{
		int res=PaperService.deletePaper(Paper.getPaperNo());
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	

}
